package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.REXTools;
import com.sxm.yiwei.utils.AESUtil;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.ValidationCode;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MinePayPasswordActivity extends BaseActivity implements View.OnClickListener{
    private String TAG ="InPayWord";
    private SharedPreferences sharedPreferences;
    private String appId;
    private ImageView paywordback;
    private EditText inpayword;
    private EditText impaycode;
    private Button paysurebt;
    private String userId;
    private String token;
    private String data;
    private String messageId;
    private String word;
    private String type =null;
    private ImageView minepsdback;
    private EditText mineciti;
    private EditText minefruit;
    private EditText mineschool;
    private Button getcode;
    private ValidationCode piccode;
    private Button minesurebt;
    private EditText minephone;
    private String phone;
    private String city;
    private String fruit;
    private String school;
    private EditText inphonecode;
    private String phonecode;
    private EditText inpiccode;
    private String pic;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_pay_password);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "0");
        token = sharedPreferences.getString("token", "2");
        getpaystates();
        initPayWordView();
    }

    private void initPayWordView() {
        minephone = (EditText) findViewById(R.id.minepay_phone);
        minepsdback = (ImageView) findViewById(R.id.minepay_psdback);
        mineciti = (EditText) findViewById(R.id.minepay_inquestionone);
        minefruit = (EditText) findViewById(R.id.minepay_inquestiontwo);
        mineschool = (EditText) findViewById(R.id.minepay_inquestionthree);
        inphonecode = (EditText) findViewById(R.id.minepay_inphonecode);
        inpiccode = (EditText) findViewById(R.id.minepay_inpiccode);
        getcode = (Button) findViewById(R.id.minepay_getcode);
        piccode = (ValidationCode) findViewById(R.id.minepay_piccode);
        minesurebt = (Button) findViewById(R.id.minepay_surebt);
        backmain = (ImageView) findViewById(R.id.minepaypsg_backmain);

        backmain.setOnClickListener(this);
        minepsdback.setOnClickListener(this);
        getcode.setOnClickListener(this);
        minesurebt.setOnClickListener(this);

    }

    // 获取短信验证码
    public void getCode() {



        RequestParams param1 = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETCODE);// 验证码接口
        param1.addHeader("Content-Type","application/json");
        param1.addHeader("appId", appId);
        param1.addHeader("userId", userId);
        param1.addHeader("token", token);
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();

        object.put("mobile",phone);
        object.put("type","3");
        param1.addParameter("params",object.toJSONString());
        x.http().get(param1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {



                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                // 获取appid
                data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                messageId = obj1.getString("messageId");

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        getInData();
        switch (v.getId()){
            case R.id.minepaypsg_backmain:
                startActivity(new Intent(MinePayPasswordActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.minepay_psdback:
                finish();
                break;
            case R.id.minepay_getcode:

                if (TextUtils.isEmpty(phone)){
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(pic)){
                    Toast.makeText(this, "请输入图形验证码", Toast.LENGTH_SHORT).show();
                }else {
                    if (REXTools.isPhoneNumber(phone)){
                        if (pic.equals(piccode.getCodeString())){
                            MyTimer timer = new MyTimer(120000,1000);
                            timer.start();
                            getCode();
                        }else {
                            Toast.makeText(this, "图形验证码错误，请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(this, "手机号错误,请核对后输入", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.minepay_surebt:
               // installPayWord();
                if (TextUtils.isEmpty(pic)){
                    Toast.makeText(this, "请输入手机验证码", Toast.LENGTH_SHORT).show();
                }else {

                    Intent intent = new Intent(MinePayPasswordActivity.this,InstrallPayPsdTwoActivity.class);
                    intent.putExtra("messageId",messageId);
                    intent.putExtra("city",city);
                    intent.putExtra("fruit",fruit);
                    intent.putExtra("school",school);
                    intent.putExtra("code",phonecode);
                    intent.putExtra("from","change");
                    intent.putExtra("url",InternetAddressUtis.CHENAGEYIVIPAYWORD);
                    startActivity(intent);
                }
                break;
        }
    }

    private void getInData() {
        phone = String.valueOf(minephone.getText());
        city = String.valueOf(mineciti.getText());
        fruit = String.valueOf(minefruit.getText());
        school = String.valueOf(mineschool.getText());
        phonecode = String.valueOf(inphonecode.getText());
        pic = String.valueOf(inpiccode.getText());

    }

    class MyTimer extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                  倒计时数        to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *           倒计时间隔               {@link #onTick(long)} callbacks.
         */
        public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getcode.setClickable(false);
            getcode.setText(millisUntilFinished/1000+"s");
        }

        @Override
        public void onFinish() {
            getcode.setClickable(true);
            getcode.setText("验证码");
        }
    }

    // 判断是否设置支付密码
    private void getpaystates() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.PAYWORDSTATES);
        params.setHeader("Content-Type","application/json");
        params.setHeader("token", token);
        params.setHeader("appId", appId);
        params.setHeader("userId", userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 支付密码--"+result);
                JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");

                JSONObject obj1 = JSON.parseObject(data);
                String isSetPassword = obj1.getString("isSetPassword");

                if (isSetPassword.equals("true")){
                    type = "3";
                }else {
                    type = "4";
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 支付密码--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
